<template>
  <div>
    <el-upload
      action="#"
      :http-request="upload"
    >
      <i class="el-icon-plus" style="width:200px; height:200px; text-ailng:center; line-height:200px; font-size:50px" />
    </el-upload>
    <canvas ref="qrcode" />
  </div>
</template>
<script>
import qrcode from 'qrcode'
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDpTryoOhkW1GEuJSJtCF0UgbFdkZ8QREq',
  SecretKey: 'OXQ4SbU7VeXAZibEbjbDZgX8TaQzIJCo'
})
export default {
  data() {
    return {
      img: '123'
    }
  },
  watch: {
    img() {
      qrcode.toCanvas(this.$refs.qrcode, this.img)
    }
  },
  mounted() {
    qrcode.toCanvas(this.$refs.qrcode, this.img)
  },
  methods: {
    upload(file) {
      console.log(file)
      cos.putObject({
        Bucket: 'rz-1306247071', /* 必须 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: file.file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: file.file, // 上传文件对象
        onProgress: function(progressData) {
          console.log(JSON.stringify(progressData))
        }
      }, (err, data) => {
        this.img = 'http://' + data.Location
        console.log(err || data)
      })
    }
  }
}
</script>
